local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    -- Asset( "IMAGE", "images/inventoryimages/panda_fisherman_supply_pack.tex" ),  -- 背包贴图
    -- Asset( "ATLAS", "images/inventoryimages/panda_fisherman_supply_pack.xml" ),
}

local function spellfn(inst,target)

    --- --- 用来手动收回 海狸
    if target ~= true then
        if inst.components.npc_everything_data:Get("ON") == true and target == nil and inst.___woodie and 
        inst.components.npc_base_lib:Distance_Targets(inst,inst.___woodie) < 20 then
            local x,y,z = inst.___woodie.Transform:GetWorldPosition()
            SpawnPrefab("npc_fx_spawn").Transform:SetPosition(x,y,z)
            inst.___woodie:Remove()
            inst.___woodie = nil
            inst.components.npc_everything_data:Set("ON",false)
            return
        end
    end



    local working_range = 15
    inst.components.npc_everything_data:Set("ON",true)
    
    if inst.___woodie == nil then        
        local temp_woodie = SpawnPrefab("woodie_beaver_npc")
        temp_woodie.Ready = true
        temp_woodie:AddTag("INLIMBO")
        temp_woodie:AddTag("notarget")
        temp_woodie:AddTag("noattack")
        temp_woodie:AddTag("invisible")
        inst.components.npc_base_lib:Add2Child(temp_woodie)
        inst.___woodie = temp_woodie
    end

    local woodie_beaver = inst.___woodie
    if not woodie_beaver:HasDebuff("npc_event_woodie_beaver") then
        woodie_beaver:AddDebuff("npc_event_woodie_beaver","npc_event_woodie_beaver")
    end
    woodie_beaver.working_range = working_range
    woodie_beaver.wereitem = inst
    if inst.components.npc_base_lib:Distance_Targets(inst,woodie_beaver) > working_range then
        local pt = inst.components.npc_base_lib:GetSpawnPoint(inst,math.random(3,working_range)) or Vector3(inst.Transform:GetWorldPosition())
        SpawnPrefab("npc_fx_spawn").Transform:SetPosition(pt.x,pt.y,pt.z)
        woodie_beaver.Transform:SetPosition(pt.x,pt.y,pt.z)
        woodie_beaver.Target_Tree = nil
        woodie_beaver.event_step = nil
    end

end

local function finiteuses_finish_fn(inst)
    if inst.___woodie then
        SpawnPrefab("npc_fx_spawn").Transform:SetPosition(inst.___woodie.Transform:GetWorldPosition())
    end
    inst:Remove()
end
-------------------------------------------------------------------------------------------------------------------------------------------------------

local function item_fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("wereitems")
    inst.AnimState:SetBuild("wereitems")
    inst.AnimState:PlayAnimation("idle_".."beaver")

    MakeInventoryFloatable(inst, "small", .2)

    inst:AddTag("item")
    inst:AddTag("woodie_beaver_wereitem")
    inst:AddTag("veryquickcast")

    inst.entity:SetPristine()
    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")

    inst:AddComponent("inspectable") --可检查组件

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem:ChangeImageName("wereitem_beaver")
        -- inst.components.inventoryitem.imagename = "panda_fisherman_supply_pack"
    -- inst.components.inventoryitem.atlasname = "images/inventoryimages/panda_fisherman_supply_pack.xml"
	inst.components.inventoryitem.cangoincontainer = true


    inst:AddComponent("named")
    -- inst.components.named:SetName(tostring(_table.Name))
    inst:DoTaskInTime(0,function()
        local language = inst.components.npc_base_lib:Get_Language()
        if language == "ch" then
            STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_woodie_beaver_wereitem")] = "召唤一只海狸Wooide来帮忙"  --人物检查的描述
            inst.components.named:SetName("特殊海狸雕像")     --- 鼠标放上去显示的名字
        else
            STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_woodie_beaver_wereitem")] = "Summon a beaver Wooide to help"  --人物检查的描述
            inst.components.named:SetName("Special Beaver Statue")     --- 鼠标放上去显示的名字
        end
    end)
    -----------------------------------------------------------------------------------------------------------------------
    inst:AddComponent("finiteuses") --- 耐久度数
    inst.components.finiteuses:SetOnFinished(finiteuses_finish_fn)
    inst.components.finiteuses:SetMaxUses(1000)
    inst.components.finiteuses:SetUses(1000)
    inst:ListenForEvent("USE",function()
        if TUNING.NPC_CONFIG.REWARDS_LESS == true then
            inst.components.finiteuses:Use(20)
        else
            inst.components.finiteuses:Use(10)
        end
    end)
    -----------------------------------------------------------------------------------------------------------------------

    inst:AddComponent("spellcaster")
    inst.components.spellcaster:SetSpellFn(spellfn)
    inst.components.spellcaster.canusefrominventory = true

    inst:DoTaskInTime(5,function()
        if inst.components.npc_everything_data:Get("ON") == true then
            spellfn(inst,true)
        end
    end)
    

    return inst
end

return Prefab("npc_item_woodie_beaver_wereitem", item_fn,assets)